package org.dream.ui.chart;

import java.util.ArrayList;
import java.util.List;

import org.dream.domain.Daydeal;
import org.dream.util.PropUtil;

/**
 * 转折点数据提供
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class StockTurnningDataSeries extends StockDataSeries {
	public StockTurnningDataSeries(List<Daydeal> daydealList) {
		truninngPoint(daydealList);
		truninngBigPoint();

	}

	/**
	 * 生成所有转折点
	 */
	private void truninngPoint(List<Daydeal> daydealList) {

		boolean trendRise = false;

		for (int i = 1; i < daydealList.size(); i++) {
			Daydeal daydeal = daydealList.get(i);
			if (trendRise) {// 如果趋势是涨的
				if (daydeal.getRiseWidth() < 0) {

					trendRise = false;// 设置趋势跌
					Daydeal daydeal2 = daydealList.get(i - 1);
					yData.add(daydeal2);
					xData.add(new Double(i));
				}
			} else {// 如果趋势是跌的
				if (daydeal.getRiseWidth() > 0) {
					trendRise = true;// 设置趋势跌
					Daydeal daydeal2 = daydealList.get(i - 1);
					yData.add(daydeal2);
					xData.add(new Double(i));
				}
			}
		}
	}

	/**
	 * 生成大的转折点 算法描述 高点：之后开始下跌，并且跌过设定的点位
	 */
	private void truninngBigPoint() {
		List<Double> xDataOld = xData;
		List<Daydeal> yDataOld = yData;
		xData = new ArrayList<Double>();
		yData = new ArrayList<Daydeal>();
		xData.add(xDataOld.get(0));
		yData.add(yDataOld.get(0));
		boolean findHeigh = yDataOld.get(1).getCLOSE() > yDataOld.get(0).getCLOSE() ? true
				: false;
		Daydeal daydealPre = yDataOld.get(0);
		for (int i = 1; i < yDataOld.size() - 1; i++) {
			Daydeal daydeal = yDataOld.get(i);// 测试是不是要找的点
			Daydeal daydealLast = yDataOld.get(i + 1);// 测试点的下一天
			double riseWidth = (daydealLast.getCLOSE() - daydeal.getCLOSE())
					/ daydeal.getCLOSE() * 100;
			if (findHeigh && riseWidth < PropUtil.getPropertyDouble("turnDownWidth")) {// 找高点

				xData.add(xDataOld.get(i));
				yData.add(yDataOld.get(i));

			}

			if (!findHeigh && riseWidth > PropUtil.getPropertyDouble("turnRiseWidth")) {// 找低点
				xData.add(xDataOld.get(i));
				yData.add(yDataOld.get(i));
			}

			daydealPre = daydeal;// 设置上一个找到点
			findHeigh = findHeigh ? false : true;
		}
	}
}
